package com.yxkj.syh.app.huarong.activities.splash;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.App;
import com.syh.app.basic.BuildConfig;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.utils.AppUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.yxkj.syh.app.huarong.util.UserManager;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivitySplashBinding;

import cn.jpush.android.api.JPushInterface;
import constant.UiType;
import model.UiConfig;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, SplashVM> {

    @Override
    protected int getContentLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected int getViewModelId() {
        return BR.splashVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTheme(R.style.Launcher);


        int width = AppUtil.getScreenWidths(this);
        int height = AppUtil.getScreenHeights(this);
        int ivPart1Height = (int) (width * 1.26f * 0.6f);
        int ivPart2Height = (int) (width * 0.6f);

        FrameLayout.LayoutParams p1 = (FrameLayout.LayoutParams) mVDBinding.ivPart1.getLayoutParams();
        FrameLayout.LayoutParams p2 = (FrameLayout.LayoutParams) mVDBinding.ivPart2.getLayoutParams();
        p1.width = (int) (width * 0.6f);
        p1.height = ivPart1Height;
        p1.topMargin = (int) (height * 0.2f);
        p2.width = width;
        p2.height = ivPart2Height;

        mVDBinding.ivPart1.setLayoutParams(p1);
        mVDBinding.ivPart2.setLayoutParams(p2);

        handler.sendEmptyMessageDelayed(1, 1000);
    }

    @Override
    protected void initData() {
        if (!BuildConfig.DEBUG) {
            CrashReport.initCrashReport(getApplicationContext(), "ded2575933", false);//bugly  初始化
        }
        UserManager.getUserManager().initUser();
        mViewModel.versionCheck();
    }


    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldVersionCheck.observe(this, versionCheck -> {

            int currentCode = AppUtil.getVersionCode(SplashActivity.this);
            if (versionCheck.getBuildVersion() > currentCode) {//要更新
                UpdateConfig updateConfig = new UpdateConfig();
                updateConfig.setForce(true);
                updateConfig.setDebug(BuildConfig.DEBUG);
                updateConfig.setApkSaveName(System.currentTimeMillis() + "-buyer");
                updateConfig.setNotifyImgRes(R.mipmap.ic_app_launcher);

                UiConfig uiConfig = new UiConfig();
                uiConfig.setUiType(UiType.SIMPLE);

                UpdateAppUtils.getInstance().apkUrl(versionCheck.getPackageUrl())
                        .updateTitle("请更新App")
                        .uiConfig(uiConfig)
                        .updateConfig(updateConfig)
                        .update();

            } else {
                if (BuildConfig.DEBUG) {
                    JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
                }
                JPushInterface.init(App.getApplication());            // 初始化 JPush
                versionChecked = true;
                jump();
            }


        });
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                delay = true;
                jump();
            }
        }
    };

    private boolean delay = false;
    private boolean versionChecked = false;

    private void jump() {
        if (delay && versionChecked) {
            if (UserManager.getUserManager().getUser() == null) {
                ARouter.getInstance().build(ArouterPath.LOGIN_ACTIVITY).navigation();
            } else {
                ARouter.getInstance().build(ArouterPath.MAIN_ACTIVITY).navigation();
            }
            finish();
        }
    }


}
